package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipPatrolCheckPlanItem;
import cc.comac.mom.bean.EquipPatrolCheckPlanItemBean;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipPatrolCheckPlanItemDao;
import cc.comac.mom.entity.EquipPatrolCheckPlanItemEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class EquipPatrolCheckPlanItemDataHandler
		extends ServiceImpl<EquipPatrolCheckPlanItemDao, EquipPatrolCheckPlanItemEntity> {
	@Autowired
	private EquipPatrolCheckPlanItemDao EquipPatrolCheckPlanItemDao;

	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
//批量新增巡检计划设备信息
	public ResultBean insertBatch(List<EquipPatrolCheckPlanItem> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list == null || list.size() < 1) {
			throw new RuntimeException("数据为空");
		}
		ArrayList<EquipPatrolCheckPlanItemEntity> listEntity = new ArrayList<EquipPatrolCheckPlanItemEntity>();
		for (EquipPatrolCheckPlanItem item : list) {
			EquipPatrolCheckPlanItemEntity innerData = item.getInnerData();
			if (innerData == null) {
				throw new RuntimeException("数据为空");
			}
			if (innerData.getDevId() == 0) {
				throw new RuntimeException("设备为空");
			}
			if (innerData.getPatrolCheckId() == 0) {
				throw new RuntimeException("巡检计划主键为空");
			}
			baseEntityDataUtil.setBaseEntityData(innerData);
			listEntity.add(innerData);
		}
		boolean saveBatch = this.saveBatch(listEntity);
		if (!saveBatch) {
			throw new RuntimeException("保存巡检计划设备信息失败");
		}
		return resultBean;
	}
//批量删除巡检设备信息
	public ResultBean deleteBatch(List<EquipPatrolCheckPlanItem> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list == null || list.size() < 1) {
			throw new RuntimeException("数据为空");
		}
		ArrayList<Long> listIds = new ArrayList<Long>();
		for (EquipPatrolCheckPlanItem item : list) {
			EquipPatrolCheckPlanItemEntity innerData = item.getInnerData();
			if (innerData == null) {
				throw new RuntimeException("数据为空");
			}
			listIds.add(innerData.getId());
		}
		List<EquipPatrolCheckPlanItemEntity> selectBatchIds = EquipPatrolCheckPlanItemDao.selectBatchIds(listIds);
		if (selectBatchIds == null || selectBatchIds.size() < 1) {
			throw new RuntimeException("删除数据不存在");
		}
		for (EquipPatrolCheckPlanItemEntity item1 : selectBatchIds) {
			baseEntityDataUtil.deleteBaseEntityData(item1);
		}
		boolean saveBatch = this.updateBatchById(selectBatchIds);
		if (!saveBatch) {
			throw new RuntimeException("删除巡检计划设备信息失败");
		}
		return resultBean;
	}

	//查询
	public ResultDev selectByFields(EquipPatrolCheckPlanItemBean obj) {
		ResultDev resultDev = new ResultDev();
		if (obj.getPageSize() == 0) {
			obj.setPageSize(10);
		}
		obj.setPageNum((obj.getPageNum()-1)*obj.getPageSize());
		List<EquipPatrolCheckPlanItemBean> listEquipRegularCheckPlanBean = EquipPatrolCheckPlanItemDao.selectByFields(obj);
		long countByFields = EquipPatrolCheckPlanItemDao.selectCountByFields(obj);
		
		// 将查询总数除以每页数量后向上取整获取总页数
		double pageCount = Math.ceil((double)countByFields / obj.getPageSize());
		resultDev.setEntityList(listEquipRegularCheckPlanBean);
		resultDev.setPageCount((int)pageCount);
		resultDev.setTotalQuantity((int)countByFields);
		return resultDev;
	}
}
